package net.hn.hnms.biz.plan.company.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.plan.company.service.CompanyService;
import net.hn.hnms.biz.plan.domain.IntegratedReporting;
import net.hn.hnms.biz.plan.mapper.IntegratedReportingMapper;
import net.hn.hnms.biz.plan.request.*;
import net.hn.hnms.biz.plan.service.IPlanMiningService;
import net.hn.hnms.biz.plan.service.IStatisticService;
import net.hn.hnms.biz.plan.vo.*;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.web.controller.BaseController;
import net.hn.hnms.sys.common.log.annotation.Log;
import net.hn.hnms.sys.common.log.enums.BusinessType;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.system.api.RemoteDeptService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Api(value = "计划模块公司端", tags = {"计划公司端"})
@RestController
@RequestMapping("/company/plan")
public class CompanyController extends BaseController {


    @Autowired
    private IPlanMiningService miningService;

    @Autowired
    private RemoteDeptService deptService;

    @Autowired
    private IntegratedReportingMapper integratedReportingMapper;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private IStatisticService statisticService;



    /**
     *  回采计划- 公司端
     * @param planMining
     * @return
     */
    @ApiOperation("获取回采计划列表公司端")
    @GetMapping("/selectMiningTotalByCompanyCodeOrMineCodeOrName")
    public R<List<PlanMiningVO>> selectTotalByMineCode(PlanMiningReq planMining) {
//        List<PlanMiningVO> planMiningVOS =null;
//        if(planMining.getCompanyCode()!=null){
//            //先根据公司编码拿公司下所属得矿，拿到矿编码  再查询矿
//            Map<String, String> map = MineInfo.getNameByCompanyCodeOfMap(planMining.getCompanyCode());
//            //拿到两个矿编码,根据矿编码汇总数据
//            planMiningVOS = companyService.selectTotalByMineCode(map);
//        }else{
//            Map<String, String> map = MineInfo.getNameByMineCodeAndName(planMining.getMineCode(),planMining.getMineName());
//            //如果名字不为空则先查询部门表 根据名字模糊查询部门表，如果结果为空，则返回空，不为空，则根据查询出来的部门信息  查询汇总数据
//            //如果 minecode 不为空 则根据 minecode 直接汇总
//            //如果两个都不为空  先根据minecode 汇总，如果为空，则返回空，不为空则根据minecode 汇总，以minecode 为优先级
//        }


        return R.ok(companyService.selectTotalByMineCode(planMining));
    }

    /**
     * 根据 公司编码或矿编码查询汇总数据
     * @param planMining
     * @return
     */
    @ApiOperation("获取掘进计划列表公司端")
    @GetMapping("/selectDrivageTotalByCompanyCodeOrMineCodeOrName")
    public R<List<PlanDrivageVO>> selectDrivageTotalByMineCode(PlanDrivageReq planMining ) {
        return R.ok(companyService.selectDrivageTotalByMineCode(planMining));
    }


    @ApiOperation("获取洗选计划列表公司端")
    @GetMapping("/selectWashingTotalByCompanyCodeOrMineCodeOrName")
    public R<List<PlanWashingVO>> selectWashingTotalByMineCode(PlanWashingReq planMining ) {
        return R.ok(companyService.selectWashingTotalByMineCode(planMining));
    }

    @ApiOperation("其他生产计划合计公司端")
    @GetMapping("/selectOtherTotalByCompanyCodeOrMineCodeOrName")
    public R<List<PlanOtherVO>> selectOtherTotalByMineCode(PlanOtherReq planMining ) {
        return R.ok(companyService.selectOtherTotalByMineCode(planMining));
    }

    @ApiOperation("回采工作面合计公司端")
    @GetMapping("/selectDimMiningFaceTotalByCompanyCodeOrMineCodeOrName")
    public R<List<DimMiningFaceVO>> selectDimMiningFaceTotalByMineCode(DimMiningFaceReq planMining ) {
        return R.ok(companyService.selectDimMiningFaceTotalByMineCode(planMining));
    }


    @ApiOperation("掘进工作面合计公司端")
    @GetMapping("/selectDimDrivageFaceTotalByCompanyCodeOrMineCodeOrName")
    public R<List<DimDrivageFaceVO>> selectDimDrivageFaceTotalByMineCode(DimDrivageFaceReq planMining ) {
        return R.ok(companyService.selectDimDrivageFaceTotalByMineCode(planMining));
    }

    @ApiOperation("产量计划合计公司端")
    @GetMapping("/selectPlanOutputTotalByCompanyCodeOrMineCodeOrName")
    public R<List<PlanOutputVO>> selectPlanOutputTotalByMineCode(PlanOutputReq planMining ) {
        return R.ok(companyService.selectPlanOutputTotalByMineCode(planMining));
    }

    @ApiOperation("进尺产量合计公司端")
    @GetMapping("/selectFootageOutputTotalByCompanyCodeOrMineCodeOrName")
    public R<List<ExecutionFootageOutputVO>> selectFootageOutputTotalByMineCode(ExecutionFootageOutputReq planMining ) {
        return R.ok(companyService.selectFootageOutputTotalByMineCode(planMining));
    }

    @ApiOperation("其他生产管理合计公司端")
    @GetMapping("/selectExecutionOtherTotalByCompanyCodeOrMineCodeOrName")
    public R<List<ExecutionOtherVO>> selectFootageOutputTotalByMineCode(ExecutionOtherReq planMining ) {
        return R.ok(companyService.selectExecutionOtherTotalByMineCode(planMining));
    }

    @ApiOperation("洗选管理合计公司端")
    @GetMapping("/selectExecutionWashingTotalByCompanyCodeOrMineCodeOrName")
    public R<List<ExecutionWashingVO>> selectExecutionWashingTotalByMineCode(ExecutionWashingReq planMining ) {
        return R.ok(companyService.selectExecutionWashingTotalByMineCode(planMining));
    }

    @ApiOperation("生产成本管理合计公司端")
    @GetMapping("/selectExecutionCostTotalByCompanyCodeOrMineCodeOrName")
    public R<List<ExecutionCostVO>> selectExecutionCostTotalByMineCode(ExecutionCostReq planMining ) {
        return R.ok(companyService.selectExecutionCostTotalByMineCode(planMining));
    }

    @ApiOperation("采掘接续回采工作面分析公司端")
    @GetMapping("/selectMiningContinuationByCompanyCodeOrMineCodeOrName")
    public R<List<MiningContinuationVo>> selectMiningContinuationByMineCode(PlanMiningReq planMining ) {
        return R.ok(companyService.selectMiningContinuationByMineCode(planMining));
    }

    @ApiOperation("采掘接续掘进工作面分析公司端")
    @GetMapping("/selectDrivageContinuationByCompanyCodeOrMineCodeOrName")
    public R<ResponsePage<DimDrivageFaceVO>> selectDrivageContinuationByMineCode(PageQuery pageQuery,PlanDrivageReq planMining ) {
        return R.ok(companyService.selectDrivageContinuationByMineCode(pageQuery,planMining));
    }
    @ApiOperation("经营综合概览生产计划合计公司端")
    @GetMapping("/completeByCompanyCodeOrMineCodeOrName")
    public  R<Map<String, PlanStatisticVO>> completeByMineCode(Integer month,String companyCode,String companyName, String mineCode ) {
        return R.ok(companyService.completeByMineCode( month,companyCode,companyName, mineCode));
    }

    @ApiOperation("经营综合概览-其他生产计划合计公司端")
    @GetMapping("/otherPlanByCompanyCodeOrMineCodeOrName")
    public  R<Map<String, PlanStatisticVO>> otherPlanByMineCode(Integer month,String companyCode,String companyName, String mineCode ) {
        return R.ok(companyService.otherPlanByMineCode( month,companyCode, companyName,mineCode));
    }


    @ApiOperation("经营综合概览-销售计划饼图公司端")
    @GetMapping("/executionSaleCount")
    @AutoSetMineCode
    public R<List<ExecutionSalesVo>> executionSaleCount(SalesPlanReq req) {
        return R.ok(statisticService.executionSaleCount(req));
    }

    @ApiOperation("经营综合概览-近6个月销量公司端")
    @GetMapping("/SaleBarChartCount")
    @AutoSetMineCode
    public R<List<ExecutionSalesVo>> SaleBarChartCount(SalesPlanReq req) {
        try {
            List<ExecutionSalesVo> resList = statisticService.SaleBarChartCount(req);
            return R.ok(resList);
        } catch (IllegalAccessException e) {

            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("北能各矿基本产销查询")
    @GetMapping("/selectProductionMarketing")
    @AutoSetMineCode
    public R<List<ProductionMarketingCountVo>> selectProductionMarketing(ProductionMarketingCountReq req) {
        return R.ok(statisticService.selectProductionMarketing(req));
    }
    @ApiOperation("北能各矿基本产销汇总")
    @GetMapping("/selectProductionMarketingCountByCoalType")
    @AutoSetMineCode
    public R<ProductionMarketingCountVo> selectProductionMarketingCountByCoalType(ProductionMarketingCountReq req) {
        return R.ok(statisticService.selectProductionMarketingCountByCoalType(req));
    }

//    @ApiOperation("经营综合概览-经营管理-运销合计公司端")
////    @SaCheckPermission("mining:face:add")
//    @GetMapping("/otherActualByCompanyCodeOrMineCodeOrName")
//    public R<ExecutionInfoVO> otherActualByCompanyCodeOrMineCodeOrName(Integer month,String companyCode,String mineCode) {
//        return R.ok(companyService.otherActualByCompanyCodeOrMineCodeOrName(month,companyCode,mineCode));
//    }





    @ApiOperation("高头窑经营综合概览-销售计划公司端")
    @GetMapping("/gtySaleCompanyPlan")
    @AutoSetMineCode
    public R<List<MonthValue>> gtySaleCompanyPlan(Integer month,String companyCode) {
        return R.ok(statisticService.gtySaleCompanyPlan(month,companyCode));
    }



    @ApiOperation("一体化填报导入")
    @PostMapping("/import")
    @Log(title = "一体化填报导入", businessType = BusinessType.IMPORT)
    public R<Void> principalImport( @RequestPart MultipartFile file)  {
        statisticService.importExcel(file,integratedReportingMapper);
        return R.ok();
    }

    @GetMapping("/down")
    @ApiOperation("一体化填报导入模板下载")
    @Log(title = "一体化填报导入模板下载", businessType = BusinessType.EXPORT)
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("一体化填报导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), IntegratedReporting.class).sheet("一体化填报导入模板").doWrite(ListUtils.newArrayList());
    }


    @ApiOperation("一体化填报信息查询")
    @PostMapping("/getIntegratedReporting")
    @Log(title = "一体化填报信息查询", businessType = BusinessType.IMPORT)
    public R<ResponsePage<IntegratedReporting>> getIntegratedReporting(@Param("日期，格式为yyyy-mm-dd") String date, String name, PageQuery pageQuery) {
        return R.ok(statisticService.getIntegratedReporting(date, name, pageQuery));
    }


    @ApiOperation("产销内购分析-月统计")
    @PostMapping("/getMonthYieldSaleStatistics")
    public R<List<IntegratedReporting>> getYieldSaleStatistics() {
        return R.ok(statisticService.getYieldSaleStatistics());
    }


    @ApiOperation("产销内购分析-日统计")
    @PostMapping("/getDayYieldSaleStatistics")
    public R<List<IntegratedReporting>> getDayYieldSaleStatistics(@RequestParam(name = "month", required = true) int month) {
        return R.ok(statisticService.getDayYieldSaleStatistics(month));
    }

}
